﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using DTO;

namespace DAL
{
    public class RegionService
    {
        CRMEntities db;
        public List<RegionEntity> LoadRegionByAll()
        {
            db = new CRMEntities();
            var query = (from re in db.Regions
                         where re.RegionLevel > 0
                         select new RegionEntity
                         {
                             Region_Code = re.Region_Code,
                             RegionLevel = re.RegionLevel,
                             RegionName = re.RegionName,
                             RegionType = re.RegionType,
                             Parent_Code = (int)re.Parent_Code,
                             Ordinal = re.Ordinal,
                         }).ToList();
            return query;
        }

        public RegionEntity LoadRegionById(int regionid)
        {
            db = new CRMEntities();
            var query = (from re in db.Regions
                         where re.Region_Code == regionid
                         select new RegionEntity
                         {
                             Region_Code = re.Region_Code,
                             RegionLevel = re.RegionLevel,
                             RegionName = re.RegionName,
                             RegionType = re.RegionType,
                             Parent_Code = (int)re.Parent_Code,
                             Ordinal = re.Ordinal,
                         }).SingleOrDefault();
            return query;
        }

        public void AddNewRegion(RegionEntity re)
        {
            db = new CRMEntities();
            Region reg = new Region();
            reg.Region_Code = re.Region_Code;
            reg.RegionLevel = re.RegionLevel;
            reg.RegionName = re.RegionName;
            reg.RegionType = re.RegionType;
            reg.Ordinal = re.Ordinal;
        }
    }
}
